#include "utype.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "gkls.h"
#include "rnd_gen.h"
#define F_real double

Resh(double *X,double *Y,double *R);
int BS(XI *X, int Jx,double *f);

int f_count;

int fibon(int i)
{
    if(i==0) return 1;
    if(i==1) return 1;
    if(i<0) return 0;
    return fibon(i-2)+fibon(i-1);
}

int Zfun3(XI *X,FUNC *z,int *NFUNC,MPAR *Af)
{
    f_count++;

    fibon(SLOW_FACTOR);

    double X1,X2,X3,X4,X5,xx,x,y,pi=3.1415926535,R1,R2,t;
    double Xp[DIMENSION];
    double a0,a1,a2,a3,a4,a5,b0,h;
    F_real GB1,GG,PDM,PG,TG,PDT,AI,GTS,GBO,BNK,T3,P3,PIT,AT,DIT,ALFA;
    //F_real ALF2,S1,S2,ALT,TT,PT,PB2,XL1,GB2,GT,TB2,ALSM,P1,P2,F1,F2,GSM,PSM,TSM,START,START1;
    char *str,str1[6];
    int  dec1,sig1,i,j,ngl;
    double B[NGL_][6]={{0.7931,0.2666,0.8736,0.1015,0.8403,0.4785},{0.2754,0.7262,0.8963,0.8171,0.9461,0.4322},{0.5737,1.0800,0.5060,0.8610,0.1420,0.4971},{0.0105,0.2951,0.7280,0.4340,0.2550,0.6028},{0.3931,0.0813,0.2185,0.5626,0.5605,0.6308},{0.3142,0.8902,0.1435,0.0690,0.2680,0.3010},{0.4567,0.6410,0.7679,0.5267,0.1170,0.9546},{0.6134,0.6990,0.1424,0.2555,0.3850,0.8783},{0.4691,0.8802,0.6429,0.6742,0.9017,0.8949},{0.9246,0.8090,0.5731,0.0900,0.6666,0.3512},{0.8740,0.0847,0.3176,0.4730,0.1557,0.0487},{0.7313,0.9234,0.7200,0.9360,0.4437,0.1440},{0.9900,0.0045,0.4707,0.7173,0.9290,0.0605},{0.2252,0.4310,0.1158,0.9437,0.6490,0.9515},{0.7130,1.0970,0.1458,0.5800,0.6900,0.6704},{0.5160,0.3460,0.0201,0.0136,0.4110,0.6005},{0.1080,0.0549,0.4822,0.2330,0.9473,0.1879},{0.9605,0.5070,0.8579,0.7263,0.3195,0.9711},{0.0960,0.6330,0.3856,0.4293,0.5789,0.3704},{0.7389,0.7272,0.0598,0.2879,0.0710,0.3150}};
    //double B21[NGL_][6]={{0.4142,0.6510,0.1300,0.7450,0.0970,0.7431},{0.8677,0.0890,0.5760,0.9863,0.4440,0.4600},{0.7720,0.4630,0.9060,0.7350,0.8520,0.5543},{0.0549,0.6950,0.8000,0.1200,0.7650,0.1800},{0.3743,0.9160,0.4351,0.5280,0.4950,0.9041},{0.2000,0.2000,0.2000,0.2000,0.2000,0.2000}};
    double B21[NGL_][6]={{0.867,0.06388,0.66,0.635,0.5391,0.989},{0.506,0.283,0.834,0.493,0.766,0.67},{0.732,0.131,0.782,0.747,0.06,0.78},{0.923,0.648,0.333,1,0.033,0.943},{0.94,0.203,0.509,0.918,0.774,0.393},{0.96,0.565,0.664,0.305,0.888,0.544},{0.51,0.057,0.956,0.149,0.714,0.877},{0.778,0.5348,0.04,0.54,0.962,0.937},{0.052,0.829,0.058,0.944,0.4,0.78},{0.242,0.651,0.933,0.862,0.224,0.768},{0.73,0.9587,0.96,0.9755,0.725,0.193},{0.065,0.516,0.973,0.60518,0.347,0.64},{0.246,0.929,0.863,0.264,0.62,0.038},{0.95758,0.847,0.202,0.72,0.303,0.94},{0.018,0.947,0.66,0.91,0.974,0.14},{0.569,0.9932,0.456,0.747,0.703,0.545},{0.722,0.776,0.542,0.067,0.55,0.114},{0.45,0.72,0.63868,0.76668,0.92,0.749},{0.986,0.954,0.068,0.805,0.52,0.563},{0.15,0.15,0.15,0.15,0.15,0.15}};
    double A_[7][7]={{0.7931,1.2666,0.8736,3.1015,0.8403,0.4785,5},{2.2754,0.7262,1.8963,0.8171,0.9461,0.4322,3},{4.5737,1.08,0.506,2.861,0.142,0.4971,2},{0.0105,5.2951,2.728,0.434,0.255,0.6028,2},{1.3931,0.0813,0.2185,0.5626,5.5605,0.6308,2},{1.3142,0.8902,2.1435,0.069,0.268,0.301,4},{0.4567,0.641,2.7679,0.5267,0.117,0.9546,1}};
    double B_[7][7]={{-0.79,4.2666,	0.8736,	3.1015,	0.8403,	0.4785,	2},{-2.2754,0.7262,	-1.8963,0.8171,	0.9461,	-0.4322,-3},{4.5737,	1.08,	-0.506,	-2.861,	0.142,	0.4971,	2},{0.0105,	-5.2951,2.728,5.434,0.255,	-0.6028,2},{1.3931,	0.0813,	-0.2185,0.5626,	0.5605,	0.6308,	-2},{1.3142,	0.8902,	-2.1435,0.069,0.268,0.301,	4},{0.4567,	-0.641,	2.7679,	0.5267,	0.117,	-0.9546,	1}};
    double C_[7][7]={{0.7931,	2.2754,	4.5737,	0.0105,	1.3931,	1.3142,	0.4567},{1.2666,	0.7262,	1.08,	5.2951,	0.0813,	0.8902,	0.641},{0.8736,	1.8963,	0.506,	2.728,	0.2185,	2.1435,	2.7679},{3.1015,	0.8171,	2.861,	0.434,	0.5626,	0.069,	0.5267},{0.8403,	0.9461,	0.142,	0.255,	5.5605,	0.268,	0.117},{0.4785,	0.4322,	0.4971,	0.6028,	0.6308,	0.301,	0.9546},{5,	3,	2,	2,	2,	4,	1}};
    double D_[7][7]={{-0.79,	-2.2754,	4.5737,	0.0105,	1.3931,	1.3142,	0.4567},{4.2666,	0.7262,	1.08,	-5.2951,	0.0813,	0.8902,	-0.641},{0.8736,	-1.8963,-0.506,	2.728,-0.2185,-2.1435,2.7679},{3.1015,	0.8171,	-2.861,	5.434,	0.5626,	0.069,	0.5267},{0.8403,	0.9461,	0.142,	0.255,	0.5605,	0.268,	0.117},{0.4785,	-0.4322,	0.4971,	-0.6028,0.6308,	0.301,-0.9546},{2,	-3,	2,	2,	-2,	4,	1}};
    double C22[4][16]={{-6.47314, -1.08618, 4.93066, -5.012245,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},{0.94775, 5.19019, -0.07813, 4.74048, 7.44678, 5.10718, 6.36621, 4.00903,0, 0, 0, 0, 0, 0, 0, 0},
                       {-5.22607, -0.58521, -7.98633, 3.54321, -3.22080, -7.13696, -7.38574, 2.09302, 2.91260, -5.79810, -0.08203, 2.40845, 0, 0, 0, 0},
                       {7.22412, 0.18140, -6.32520, -4.76050, 7.36377, -4.44949, 5.63477, 7.33618, 5.08154, 1.06226, 7.54785, 5.44849, 6.12744, -6.53638, 7.16406, -7.67358}};
    double G[NGL_];
    double g23[5]={2,2,2,20,2};
    double B23[5][DIMENSION];
    double S[NGL_];
    double Ax1, Ay1, Ax2, Ay2,zero=0.0,Lmax;
    double A[DIMENSION]={0.51234,0.24565},u[DIMENSION];
    //double A[DIMENSION]={0.51234},u[DIMENSION];
    x=(*X)[0]; y=(*X)[1];
    //
    (*Af).A[0][4]=(*Af).A[0][4]+1;

    if((*NFUNC)==0)
    {
        Ax1=(*Af).A[0][0]; Ay1=(*Af).A[1][0];
        Ax2=(*Af).A[0][1]; Ay2=(*Af).A[1][1];
        Ax1=0.5393536;
        Ay1=0.1423383;
        Ax2=0.4620808;
        Ay2=0.2353282;
        *z=1.0/((x-Ax1)*(x-Ax1)+(y-Ay1)*(y-Ay1));
        *z=(*z)+1.0/((x-Ax2)*(x-Ax2)+(y-Ay2)*(y-Ay2));
        return(1);
    }
    if((*NFUNC)==2)
    {
        // ,       x=1, y=1
        //	X1=2.56*((x)-1.28;
        //	X2=2.56*((*X0)[1])-1.28;
        X1=2*(x)-0.1;
        X2=2*(y)-0.1;
        (*z)=100.0/(100.0*(X1*X1-X2)*(X1*X1-X2)+(1.0-X1)*(1.0-X1)+1.0);
        //	(*z)=1.0/(1.0*(X1*X1-X2)*(X1*X1-X2)+(1.0-X1)*(1.0-X1)+1.0);
        return(1);
    }
    if((*NFUNC)==1)
    {
        (*z)=-(1/(16*(xx-2.1)*(xx-2.1)-1)+1/(10*(xx-0.1)*(xx-0.1)+10)+1/(1*(xx-1.5)*(xx-1.5)+2));
        (*z)=(y)-(1/(30*(xx-3.7)*(xx-3.7)+0.5)+1/(20*(xx-0.7)*(xx-0.7)+0.9)+1/(30*(xx-0.9)*(xx-0.9)+0.7));
        return(1);
    }
    if((*NFUNC)==3)
    {
        A[0]=0.51234;
        A[1]=0.24565;
        A[2]=0.151234;//0.105;
        A[3]=0.7655;//0.51234;//0.7655;
        //	A[4]=0.3455;
        //	A[5]=0.83455;
        //	A[6]=0.455;
        //	A[7]=0.2455;
        //	A[8]=0.5455;
        //	A[9]=0.6455;
        Ax1=100;
        //	for(i=0;i<DIMENSION;i++) (*X)[i]=A[i];
        for(i=0;i<DIMENSION;i++) u[i]=((*X)[i]+0.5125);
        (*z)=0;
        for(i=0;i<DIMENSION-1;i++)
        {(*z)=(*z)+Ax1*(u[i]*u[i]-u[i+1])*(u[i]*u[i]-u[i+1]);
            Ax1=Ax1/5;
        }
        (*z)=(*z)+(1-u[DIMENSION-1])*(1-u[DIMENSION-1])+1;
        (*z)=100.0/(*z);
        Ax1=0;
        for(i=0;i<DIMENSION;i++)
            Ax1=Ax1+(u[i]-A[i]-0.5125)*(u[i]-A[i]-0.5125);

        (*z)=(*z)+1.0/(1.0-exp(-Ax1/0.1));
        //(*z)=100.0/(100.0*(X1*X1-X2)*(X1*X1-X2)+(1.0-X1)*(1.0-X1)+1.0);
        //
        //(*z)=(*z)+200*exp(-((X1-0.21)*(X1-0.21)+(X2-1.51)*(X2-1.51))/0.01);
        //(*z)=(*z)+1.0/(1.0-exp(-((X1-0.21)*(X1-0.21)+(X2-1.51)*(X2-1.51))/0.01));
        //(*z)=(*z)+1.0/(1.0-exp(-((X1- Ax1)*(X1-Ax1 )+(X2-Ay1 )*(X2- Ay1))/0.01));
        Ax2=((*X)[0]-A[0])*((*X)[0]-A[0])+((*X)[1]-A[1])*((*X)[1]-A[1])+((*X)[2]-A[2])*((*X)[2]-A[2]);
        Ax2=sqrt(Ax2);
        //if(Ax2<0.01)
        if((*z)>55000.)
        {
            printf("kkkk z=%e S=%e\n",*z,Ax2);
            //getch();
            printf("begin\n");
        }

        return(1);
    }
    if((*NFUNC)==4)
    {
        //Griewank
        //	X1=40*((*X0)[0])-20;
        //	X2=40*((*X0)[1])-20;
        Ax1=10.0*((*Af).A[0][0])-5.0;
        Ay1=10.0*((*Af).A[1][0])-5.0;
        Ax1=12.12345;
        Ay1=4.434212;
        X1=40*(x)-20.0+Ax1;
        X2=40*(y)-20.0+Ay1;
        (*z)=1.0/((X1*X1+X2*X2)/200.0-cos(X1)*cos(X2/1.41)+0.1);
        //(*z)=-((X1*X1+X2*X2)/200.0-cos(X1)*cos(X2/1.41)+1);
        return(1);
    }
    if((*NFUNC)==5)
    {
        //Griewank+
        //	X1=40*((*X0)[0])-20;
        //	X2=40*((*X0)[1])-20;
        //	Ax1=10.0*((*Af).A[0][0])-5.0;
        //	Ay1=10.0*((*Af).A[1][0])-5.0;
        Ax1=20;
        Ay1=20;
        X1=40*(x)-Ax1;
        X2=40*(y)-Ay1;
        (*z)=1.0/((X1*X1+X2*X2)/200.0-cos(X1)*cos(X2/1.41));
        (*z)=fabs(*z);
        return(1);
    }
    if((*NFUNC)==6)
    {
        //2
        //
        X1=10.24*(x)-4.;
        X2=10.24*(y)-4.5;
        (*z)=(10.0*cos(1.*pi*X1)-X1*X1)+(10.0*cos(1.*pi*X2)-X2*X2)-20;
        return(1);
    }

    if((*NFUNC)==7)
    {
        //2+
        Ax1=5.0*((*Af).A[0][0])-2.5;
        Ay1=5.0*((*Af).A[1][0])-2.5;
        X1=10.*(x)-5.+Ax1;
        X2=10.*(y)-5.+Ay1;
        (*z)=1/((10.0*cos(1.*pi*X1)-X1*X1)+(10.0*cos(1.*pi*X2)-X2*X2));
        //(*z)=((10.0*cos(1.*pi*X1)-X1*X1)+(10.0*cos(1.*pi*X2)-X2*X2));
        return(1);
    }
    if((*NFUNC)==8)
    {
        //2++
        Ax1=(*Af).A[0][0]; Ay1=(*Af).A[1][0];
        //	Ax1=0.304; Ay1=0.015;
        Ax1=10.24*Ax1-5.12;
        Ay1=10.24*Ay1-5.12;
        X1=10.24*(x)-5.12;
        X2=10.24*(y)-5.12;
        (*z)=((10.0*cos(1.*pi*X1)-X1*X1)+(10.0*cos(1.*pi*X2)-X2*X2)-20);
        (*z)=(*z)+1.0/(1.0-exp(-((X1-Ax1)*(X1-Ax1)+(X2-Ay1)*(X2-Ay1))/0.01));
        return(1);
    }
    if((*NFUNC)==9)
    {
        //1
        X1=(x);
        X2=(y);
        (*z)=2*X1+X2;
        (*z)=(*z)+1.0/(1.0-exp(-((X1-0.31)*(X1-0.31)+(X2-0.35)*(X2-0.35))/0.01));
        return(1);
    }
    if((*NFUNC)==10)
    {
        //1
        A[0]=0.51234;
        A[1]=0.24565;
        A[2]=0.151234;//0.105;
        A[3]=0.7655;//0.51234;//0.7655;
        //	A[4]=0.3455;
        //	A[5]=0.83455;
        //	A[6]=0.455;
        //	A[7]=0.2455;
        //	A[8]=0.5455;
        //	A[9]=0.6455;
        for(i=0;i<DIMENSION;i++) u[i]=(*X)[i];

        (*z)=u[0]*u[0]*u[0];
        for(i=0;i<DIMENSION-1;i++)
            (*z)=(*z)+(i+2)*u[i]*u[i+1];

        (*z)=(*z)+u[DIMENSION-1]*u[DIMENSION-1];

        Ax1=0;
        for(i=0;i<DIMENSION;i++)
            Ax1=Ax1+(u[i]-A[i])*(u[i]-A[i]);

        (*z)=(*z)+1.0/(1.0-exp(-Ax1/0.1));

        return(1);
    }
    if((*NFUNC)==11)
    {
        X1=2.3111*(x)-1.011;
        X2=2.3222*(y)-1.011;
        (*z)=fabs(X2*X2-4.0*X1);
        (*z)=(-X2+sqrt(*z))/X1;
        return(1);
    }
    if((*NFUNC)==12)
    {
        //

        GB1=80;
        GG=50;//100;
        PDM=0.95;

        PG=1950*(x)+50;//140;
        TG=1950*(y)+50;//1000;

        PDT=0.89;
        AI=150;
        GTS=30;
        GBO=10;
        BNK=9000;
        //f1=1.0;
        //if((TG.a>300)&&(TG.a<350)) GG=1.0/(1.0-f1);
        //GG=log(0.0-f1);

        //TURBI(&GB1,&GG,&PDM,&PG,&TG,&PDT,&AI,&GTS,&GBO,&BNK, &T3,&P3,&PIT,&AT,&DIT,&ALFA);

        (*z)=T3*T3+P3*P3+PIT*PIT+AT*AT;
        return(1);

    }
    /*
if((*NFUNC)==13) 
 {
//  
ALF2=0; 
S1=0.99;
S2=0.975;
ALT=2.7;
TT=1100*(x)+200;//744.;
//TT=890*(x)+10;//744.;
//PT=80;
PT=300*(y)+10;//200.;
PB2=208.;
GB2=103.5;
GT=54.4;
TB2=383;
GTS=72.68;
XL1=0.16;

SMESH(&ALF2,&S1,&S2,&ALT,&TT,&PT,&PB2,&XL1,&GB2,&GT,&TB2,&GTS,
  &ALSM,&P1,&P2,&F1,&F2,&GSM,&PSM,&TSM,&START,&START1);
//(*z)=P1*P1/2.0e4+P2*P2/2.0e4+F1*F1/5.0e8+F2*F2/5.0e6+GSM*GSM/2e4+PSM*PSM/2e4+TSM*TSM/3e4;
(*z)=PSM;
//fprintf((*(PFILE *)(pstream)),"%e %e %e %e %e %e %e\n",TT,PT,F1,F2,GSM,PSM,TSM);
//r=ecvt((double)(*z), 5, &dec1, &sig1);
strcpy(str1,ecvt((double)(*z), 5, &dec1, &sig1));
if (strcmp(str1,"1#IND")==0)  (*z)=1.0e77;
*/
    /*if(*z>2050.)
 {printf("x=%f,y=%f,*z=%f,TT=%e PT=%e P1=%e P2=%e F1=%e F2=%e PSM=%e\n",x,y,*z,TT,PT,P1,P2,F1,F2,PSM);
 getch();}
*/ 
    /*
return(1);  
}
*/
    if((*NFUNC)==14)
    {
        A[0]=0.51234;
        A[1]=0.24565;
        A[2]=0.151234;//0.105;
        A[3]=0.7655;//0.51234;//0.7655;
        //	A[4]=0.3455;
        //	A[5]=0.83455;
        //	A[6]=0.455;
        //	A[7]=0.2455;
        //	A[8]=0.5455;
        //	A[9]=0.6455;
        //    Ax1=(*Af).A[0][0];
        //    Ay1=(*Af).A[1][0];
        Ax1=0.611;
        Ay1=0.392;
        //	for(i=0;i<DIMENSION;i++) (*X)[i]=A[i];
        for(i=0;i<DIMENSION;i++) u[i]=1.-(11.-20.0*((*X)[i]-A[i]+0.55))/4.0;

        (*z)=10.0*sin(pi*u[0])*sin(pi*u[0]);
        for(i=1;i<DIMENSION-1;i++)
            (*z)=(*z)+((u[i]-1.)*(u[i]-1.)*(1+10*sin(pi*u[i+1])*sin(pi*u[i+1])));
        (*z)=(*z)+(u[DIMENSION-1]-1.)*(u[DIMENSION-1]-1.);

        //(*z)=-(*z);
        (*z)=1.0/(*z);

        return(1);
    }

    if((*NFUNC)==15)
    {
        //Griewank
        //	X1=40*((*X0)[0])-20;
        //	X2=40*((*X0)[1])-20;
        //	Ax1=10.0*((*Af).A[0][0])-5.0;
        //	Ay1=10.0*((*Af).A[1][0])-5.0;
        Ax1=12.12345;
        Ay1=4.434212;
        X1=40*(*X)[0]-20.0+Ax1;
        X2=40*(*X)[1]-20.0+Ay1;
        X3=40*(*X)[2]-20.0-8.4563;
        X4=40*(*X)[3]-20.0-0.14563;
        X5=40*(*X)[4]-20.0+2.14563;
        if(DIMENSION==2)
            (*z)=1.0/((X1*X1+X2*X2)/400.0-cos(X1)*cos(X2/1.41)+0.1);
        if(DIMENSION==3)
            (*z)=1.0/((X1*X1+X2*X2+X3*X3)/800.0-cos(X1)*cos(X2/1.41)*cos(X3/1.732)+0.1);
        if(DIMENSION==4)
            (*z)=1.0/((X1*X1+X2*X2+X3*X3+X4*X4)/1200.0-cos(X1)*cos(X2/1.41)*cos(X3/1.732)*cos(X4/2)+0.1);
        if(DIMENSION==5)
            (*z)=1.0/((X1*X1+X2*X2+X3*X3+X4*X4+X5*X5)/1600.0-cos(X1)*cos(X2/1.41)*cos(X3/1.732)*cos(X4/2)*cos(X5/2.24)+0.1);
        //(*z)=-((X1*X1+X2*X2)/200.0-cos(X1)*cos(X2/1.41)+1);
        return(1);
    }

    if((*NFUNC)==16)
    {
        //

        X1=2*(*X)[0];
        X2=2*(*X)[1];
        Resh(&X1,&X2,z);
        if((*z)<1)
            (*z)=GRAN+(*z);
        return(1);
    }
    if((*NFUNC)==17)
    {
        //
        //  [-al+c,al+c]: ~x=2alx-al+c
        a0=2*0.05*(*X)[0]+1.-0.05;
        a1=2*0.05*(*X)[1]+1-0.05;
        a2=2*0.1*(*X)[2]+0.49-0.1;
        a3=2*0.1*(*X)[3]+0.16-0.1;
        a4=2*0.025*(*X)[4]+0.043-0.025;
        a5=2*0.025*(*X)[5]+0.008-0.025;
        //	a0=1.0000434;
        //	a1=1.0000217;
        //	a2=0.49921875;
        //	a3=0.16649306;
        //	a4=0.04375;
        //	a5=0.00868056;

        h=2.0/501; x=-1.0; y=-1.0e77;
        for(i=0;i<=501;i++)
            //		{(*z)=fabs(sqrt(1+x)-(a0+a1*x+a2*x*x+a3*x*x*x+a4*x*x*x*x+a5*x*x*x*x*x));
        {(*z)=fabs(exp(x)-(a0+a1*x+a2*x*x+a3*x*x*x+a4*x*x*x*x+a5*x*x*x*x*x));
            if((*z)>y) y=(*z);
            x=x+h;
        }
        (*z)=1./y;
        //	if((*z)>321)
        //		(*z)=1/(*z);
        return(1);
    }
    if((*NFUNC)==18)
    {
        //Griewank
        //	X1=40*((*X0)[0])-20;
        //	X2=40*((*X0)[1])-20;
        //	Ax1=10.0*((*Af).A[0][0])-5.0;
        //	Ay1=10.0*((*Af).A[1][0])-5.0;
        //  x=0.49691 y=0.486698
        //(*X)[0]=0.45;(*X)[1]=0.45;
        Ax1=2;//0.12345;//12.12345;
        Ay1=2;//0.5321;//4.434212;
        X1=40*(*X)[0]-20.0+Ax1;
        X2=40*(*X)[1]-20.0+Ay1;
        X3=40*(*X)[2]-20.0-0.4563;
        X4=40*(*X)[3]-20.0-0.14563;
        X5=40*(*X)[4]-20.0+1.14563;
        if(DIMENSION==2)
            (*z)=10-((X1*X1+X2*X2)/400.0-cos(X1)*cos(X2/1.41)+1.0);
        if(DIMENSION==3)
            (*z)=10-((X1*X1+X2*X2+X3*X3)/400.0-cos(X1)*cos(X2/1.41)*cos(X3/1.732)+1.);
        if(DIMENSION==4)
            (*z)=10-((X1*X1+X2*X2+X3*X3+X4*X4)/400.0-cos(X1)*cos(X2/1.41)*cos(X3/1.732)*cos(X4/2)+1.0);
        if(DIMENSION==5)
            (*z)=10-((X1*X1+X2*X2+X3*X3+X4*X4+X5*X5)/400.0-cos(X1)*cos(X2/1.41)*cos(X3/1.732)*cos(X4/2)*cos(X5/2.24)+1.);
        //(*z)=-((X1*X1+X2*X2)/200.0-cos(X1)*cos(X2/1.41)+1);
        //
        //	for(i=0;i<64000;i++) xx=exp(pi);
        return(1);
    }
    if((*NFUNC)==19)
    {
        //
        //	for (i=0;i<DIMENSION;i++)
        //		(*X)[i]=B[19][i];
        //	(*X)[0]=0.72304;
        //	(*X)[1]=0.73746;
        ngl=20;
        (*z)=0;
        for (i=0;i<ngl;i++)
        {y=0;
            for (j=0;j<DIMENSION;j++)
                y=y+((*X)[j]-B[i][j])*((*X)[j]-B[i][j]);
            (*z)=(*z)+(i+1)*exp((-1)*y/0.01);
        }
        //	for(i=0;i<64000;i++) xx=exp(pi);
        return(1);
    }

    if((*NFUNC)==20)
    {
        //
        //(*X)[0]=0.5;
        //(*X)[1]=0.5;
        a0=0;b0=0;
        (*z)=0;
        for (i=0;i<7;i++)
            for (j=0;j<7;j++)
            {a0=a0+A_[i][j]*sin((i+1)*pi*(*X)[0])*sin((j+1)*pi*(*X)[1])+B_[i][j]*cos((i+1)*pi*(*X)[0])*cos((j+1)*pi*(*X)[1]);
                b0=b0+C_[i][j]*sin((i+1)*pi*(*X)[0])*sin((j+1)*pi*(*X)[1])+D_[i][j]*cos((i+1)*pi*(*X)[0])*cos((j+1)*pi*(*X)[1]);
            }
        (*z)=a0*a0+b0*b0;
        (*z)=sqrt((*z));
        //	for(i=0;i<64000;i++) xx=exp(pi);
        return(1);
    }
    if((*NFUNC)==21)
    {
        //
        (*Af).A[1][0]=0.028331858272042;
        (*Af).A[1][1]=0.030260628070925;
        (*Af).A[1][2]=0.0019882808923612;
        (*Af).A[1][3]=-0.019804986724448;
        Lmax=24;

        for (i=0;i<NGL_;i++)
            for (j=0;j<DIMENSION;j++)
            {B[i][j]=10.0*B21[i][j];}
        for (j=0;j<DIMENSION;j++)
        {Xp[j]=10.0*((*X)[j]-(*Af).A[1][j]);}

        for (i=0;i<NGL_-1;i++)

            G[i]=2.7;

        G[19]=G[18]+0.5*Lmax*sqrt(DIMENSION-1+0.25)+1;

        for (i=0;i<NGL_-1;i++)
            S[i]=0.25;

        S[19]=(2*G[19]*G[19])/(exp(1)*Lmax*Lmax);
        (*z)=0;
        for (i=0;i<NGL_;i++)
        {y=0;
            for (j=0;j<DIMENSION;j++)
                y=y+(Xp[j]-B[i][j])*(Xp[j]-B[i][j]);
            (*z)=(*z)+G[i]*exp((-1)*y/S[i]);
        }

        //(*z)=(*z)*(*z)*(*z)*(*z);
        /*y=G[5];
 for (j=0;j<DIMENSION;j++)
  {
  BS(X,j,&b0);
  y=y*b0;
  }
 (*z)=(*z)+y;*/

        //	for(i=0;i<64000;i++) xx=exp(pi);
        return(1);
    }
    if((*NFUNC)==22)
    {
        for (j=0;j<DIMENSION;j++)
        {Xp[j]=2.0*((*X)[j])-1.0;}
        R1=1; R2=1;
        for (j=0;j<DIMENSION;j++)
        {
            x=C22[DIMENSION-1][j]*Xp[j]+C22[DIMENSION-1][j+DIMENSION];
            y=C22[DIMENSION-1][2*DIMENSION+j]*Xp[j]+C22[DIMENSION-1][3*DIMENSION+j];
            R1=R1*cos(x); R2=R2*cos(y);
        }
        (*z)=R1+R2; (*z)=-(*z);
        return(1);
    }
    if((*NFUNC)==23)
    {
        a0=0.014142136;
        a1=a0*sqrt(DIMENSION);

        //for(i=0;i<DIMENSION;i++)
        //{(*X)[i]=0.25;}
        //(*X)[0]=0.75;

        for (j=0;j<DIMENSION;j++)
            Xp[j]=(*X)[j];

        for (i=0;i<DIMENSION;i++)
        {
            B23[0][i]=0.65432176;
            B23[1][i]=0.2012345;
            B23[2][i]=0.517654;
            B23[3][i]=0.2312345;
            B23[4][i]=0.83432176;
        }
        B23[3][0]=0.675432176;
        B23[4][0]=0.12312345;
        y=0.0;
        for (i=0;i<5;i++)
        {
            t=0;
            for (j=0;j<DIMENSION;j++)
                t=t+(Xp[j]-B23[i][j])*(Xp[j]-B23[i][j]);

            t=(-1)*t/a1;
            y=y+g23[i]*exp(t);
        }
        for (j=0;j<DIMENSION;j++)
            y=y-4*(Xp[j]-0.5)*(Xp[j]-0.5);
        (*z)=y;	
        return(1);
    }
    if((*NFUNC)==24)
    {
        //(*X)[0]=0.75;(*X)[1]=0.5625;(*X)[2]=0.625;(*X)[3]=0.5625;(*X)[4]=0.5625;
        //(*X)[5]=0.125;(*X)[6]=0.25;(*X)[7]=0.4375;(*X)[8]=0.0625;(*X)[9]=0.875;
        //(*X)[10]=0.5;(*X)[11]=0.625;(*X)[12]=0.4375;(*X)[13]=0.0625;(*X)[14]=0.5;
        for (j=0;j<DIMENSION;j++)
            Xp[j]=2*((*X)[j])-1;
        if ((*Af).tip==1) (*z)=GKLS_D_func(&Xp);
        if ((*Af).tip==3) (*z)=GKLS_ND_func(&Xp); //-- for ND-type test function
        if ((*Af).tip==2) (*z)=GKLS_D2_func(&Xp); //-- for D2-type test function
        //GKLS_GR2(Af,&Xp,z);
        (*z)=-(*z);
        return(1);
    }

    return(1);
}



int BS(XI *X,int Jx,double *f)
{ double x0,x1,x2,h;
    h=0.2;
    x0=0.2-h;
    x1=0.2;
    x2=0.2+h;
    *f=0;
    if (((*X)[Jx]>=x0) &  ((*X)[Jx]<x2))
    {(*f)=(*f)+((*X)[Jx]-x0)/h;}

    if (((*X)[Jx]>=x1) &  ((*X)[Jx]<x2))
    {(*f)=(*f)-2*((*X)[Jx]-x1)/h;}
    return(1);
}
